package com.research.project.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.research.common.core.domain.entity.SysUser;
import com.research.common.core.domain.model.LoginUser;
import com.research.common.utils.SecurityUtils;
import com.research.project.domain.Project;
import com.research.project.dto.ProjectDocDto;
import com.research.project.mapper.ProjectMapper;
import com.research.project.vo.ProjectDocVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.research.project.mapper.ProjectDocMapper;
import com.research.project.domain.ProjectDoc;
import com.research.project.service.IProjectDocService;

/**
 * 项目文档Service业务层处理
 * 
 * @author ghn
 * @date 2025-09-07
 */
@Service
@Slf4j
public class ProjectDocServiceImpl implements IProjectDocService 
{
    @Autowired
    private ProjectDocMapper projectDocMapper;
    @Autowired
    private ProjectMapper projectMapper;

    /**
     * 查询项目文档
     * 
     * @param id 项目文档主键
     * @return 项目文档
     */
    @Override
    public ProjectDocVo selectProjectDocById(Long id)
    {
        ProjectDoc projectDoc = projectDocMapper.selectProjectDocById(id);
        String name = projectMapper.selectNameById(projectDoc.getProjectId());
        ProjectDocVo projectDocVo = new ProjectDocVo();
        BeanUtils.copyProperties(projectDoc,projectDocVo);
        projectDocVo.setProjectName(name);
        return projectDocVo;
    }

    /**
     * 查询项目文档列表
     * 
     * @param projectDocDto 项目文档
     * @return 项目文档
     */
    @Override
    public List<ProjectDocVo> selectProjectDocList(ProjectDocDto projectDocDto)
    {
        ProjectDoc projectDoc = new ProjectDoc();
        BeanUtils.copyProperties(projectDocDto,projectDoc);
        if(projectDocDto.getProjectName()!=null&&projectDocDto.getProjectName()!=""){
            //不为空，说明在查询时加入了名称
            Project project = projectMapper.selectByName(projectDocDto.getProjectName());//返回根据名称找到的项目
            projectDoc.setProjectId(project.getId());//将项目的id注入，以便补充查询条件。
        }

        List<ProjectDoc> projectDocs = projectDocMapper.selectProjectDocList(projectDoc);//根据项目
        List<ProjectDocVo> projectDocVos = new ArrayList<>();
        for (ProjectDoc doc : projectDocs) {
            //根据项目id查找对应的名称，然后进行赋值。
            String name = projectMapper.selectNameById(doc.getProjectId());
            ProjectDocVo projectDocVo = new ProjectDocVo();
            BeanUtils.copyProperties(doc,projectDocVo);
            projectDocVo.setProjectName(name);
            projectDocVos.add(projectDocVo);
        }
        return projectDocVos;
    }

    /**
     * 新增项目文档
     * 
     * @param projectDocDto 项目文档
     * @return 结果
     */
    @Override
    public int insertProjectDoc(ProjectDocDto projectDocDto)
    {
        //根据名称查找出对应的项目
        String projectName = projectDocDto.getProjectName();
        Project project = projectMapper.selectByName(projectName);

        //根据查到的项目进行赋值
        ProjectDoc projectDoc = new ProjectDoc();
        BeanUtils.copyProperties(projectDocDto,projectDoc);
        projectDoc.setProjectId(project.getId());

        //log.info("projectDocDto:{}",projectDocDto);
        //log.info("projectDoc:{}",projectDoc);

        //对插入人进行赋值
        LoginUser loginUser = SecurityUtils.getLoginUser();
        projectDoc.setUploaderId(loginUser.getUserId());
        projectDoc.setUploaderName(loginUser.getUsername());
        projectDoc.setUploadTime(DateTime.now());


        return projectDocMapper.insertProjectDoc(projectDoc);
    }

    /**
     * 修改项目文档
     * 
     * @param projectDoc 项目文档
     * @return 结果
     */
    @Override
    public int updateProjectDoc(ProjectDoc projectDoc)
    {
        return projectDocMapper.updateProjectDoc(projectDoc);
    }

    /**
     * 批量删除项目文档
     * 
     * @param ids 需要删除的项目文档主键
     * @return 结果
     */
    @Override
    public int deleteProjectDocByIds(Long[] ids)
    {
        return projectDocMapper.deleteProjectDocByIds(ids);
    }

    /**
     * 删除项目文档信息
     * 
     * @param id 项目文档主键
     * @return 结果
     */
    @Override
    public int deleteProjectDocById(Long id)
    {
        return projectDocMapper.deleteProjectDocById(id);
    }
}
